/*
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      Indexer.hpp
 *
 * $FTS$
 */
#ifndef _INDEXER_HPP__
#define _INDEXER_HPP__ 1

/**
  @file Indexer.hpp
  @brief Data indexer class
*/

// Local Includes

#include <Hash.hpp>
#include <HashFunctions.hpp>
#include <InvIndexStorage.hpp>
#include <InvIndexBPlusStorage.hpp>

// C++ Includes
#ifdef _NO_FTSS_STL_ANALOGS
  #include <STLVector.hpp>
#else
  #include <DocumentEntryContainer.hpp>
#endif // _NO_FTSS_STL_ANALOGS

namespace FTSS // FullTextSearchSystem
{

/**
  @class Indexer Indexer.hpp <Indexer.hpp>
  @brief Data indexer class
*/
class Indexer
{
public:
	/**
	  @brief Constructor
	  @param pIStorage - Interface to data storage
	  @param iDictSize - Initial dictionary size
	*/
//	Indexer(InvIndexStorage * pIStorage, const Size_t & iDictSize = (1 << 0));
	Indexer(InvIndexBPlusStorage * pIStorage, const Size_t & iDictSize = (1 << 0));

	/**
	  @brief Start indexing
	  @param iStartDocumentId - Initial document ID
	*/
	void StartIndexer(const DocumentId_t & iStartDocumentId = 0);

	/**
	  @brief Start new document
	  @param sDocName - Document Name
	  @param iDocNameLen - Document Name Length
	  @return Id of new document
	*/
	DocumentId_t StartDocument(CCHAR_P sDocName, const UINT_32 & iDocNameLen);

	/**
	  @brief Store word in index
	  @param iWordHash - word hash
	  @param iWordForm - word form, inherited from AOT's lemmatizer
	  @param iZoneId - Zone ID
	  @param iWordPos - Position in document
	*/
	void PushWord(const TokenHash_t   & iWordHash,
	              const WordForm_t    & iWordForm,
	              const ZoneId_t      & iZoneId,
	              const DocumentPos_t & iWordPos);

	/**
	  @brief End of document
	*/
	void EndDocument();

	/**
	  @brief Finish of entire document collection
	*/
	void StopIndexer();

	/** A Destructor */
	~Indexer() throw();

private:
	/** Data storage class          */
	//InvIndexStorage      * pStorage;
	InvIndexBPlusStorage      * pStorage;
	/** Current document ID         */
	TokenHash_t            iDocumentId;
	/** Document number             */
	TokenHash_t            iDocumentNum;
	/** Number of indexed documents */
	Size_t                 iDocumentCount;

#ifdef _NO_FTSS_STL_ANALOGS
	/**
	  @typedef Pair<TokenHash_t, STLW::vector<WordEntry> > IndexPair
	  @brief Index pair type definition
	*/
	typedef Pair<TokenHash_t, STLW::vector<WordEntry> > IndexPair;
#else
	/**
	  @typedef Pair<TokenHash_t, DocumentEntryContainer > IndexPair
	  @brief Index pair type definition
	*/
	typedef Pair<TokenHash_t, DocumentEntryContainer> IndexPair;
#endif // _NO_FTSS_STL_ANALOGS

#ifdef _NO_FTSS_STL_ANALOGS
	/**
	  @typedef Hash<TokenHash_t, STLW::vector<WordEntry>, HashHasher, HashComparator> IndexHash
	  @brief Index hash type definition
	*/
	typedef Hash<TokenHash_t, STLW::vector<WordEntry>, HashHasher, HashComparator> IndexHash;
#else
	/**
	  @typedef Hash<TokenHash_t, DocumentEntryContainer, HashHasher, HashComparator> IndexHash
	  @brief Index hash type definition
	  <pre>
		STL Analog

			word0 -> { [doc_id0,
			            next,
			            [we1, we2 .... weN]
			           ],
			           [doc_id1,
			            next,
			            [we1, we2 .... weN]
			           ],
			           .....
			           [doc_idn,
			            next,
			            [we1, we2 .... weN]
			           ]
			         }

			word2 -> { [doc_id0,
			            next,
			            [we1, we2 .... weN]
			           ],
			           [doc_id3,
			            next,
			            [we1, we2 .... weN]
			           ],
			           [doc_id6,
			            next,
			            [we1, we2 .... weN]
			           ]
			         }
	  </pre>
	  STLW::map<STLW::string, STLW::vector<DocumentEntry> >
	  We cannot use STL because of performance penalty
	*/
	typedef Hash<TokenHash_t, DocumentEntryContainer, HashHasher, HashComparator> IndexHash;
#endif // _NO_FTSS_STL_ANALOGS

	/** Index hash          */
	IndexHash       oIndexHash;
};

} // namespace FTSS
#endif // _INDEXER_HPP__
// End.
